package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public abstract class HelperBase {

	protected ApplicationManager manager;
	public WebDriver driver;
	private WebDriverWait wait;

	public HelperBase(ApplicationManager manager) {
		this.manager = manager;
		this.driver = manager.driver;
		wait = new WebDriverWait(driver, 30);
	}
	
	protected List<WebElement> getElements(By locator) {
		wait.until(visibilityOfElementLocated(locator));
		List<WebElement> elements = driver.findElements(locator);
		return elements;
	}
	
	public boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	protected void type(By locator, String text) {
		WebElement element = wait.until(visibilityOfElementLocated(locator));
		if (text != null) {
		element.clear();
		element.sendKeys(text);	
		}
	}

	protected void click(By locator) {
		wait.until(visibilityOfElementLocated(locator)).click();
	}
	
	protected void select(By locator, String optionText) {
		WebElement element = wait.until(visibilityOfElementLocated(locator));
		if (optionText != null) {
			new Select(element).selectByVisibleText(optionText);	
	}
}
	protected void selectByNumber(By locator, int index){
		WebElement element = wait.until(visibilityOfElementLocated(locator));
		new Select(element).selectByIndex(index);
	}
}
